package com.zlzvip.android.app;


import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.pachong.android.framework.SecretCodeActivity;
import com.pachong.android.frameworkbase.utils.DimensionUtil;
import com.tencent.bugly.crashreport.CrashReport;
import com.uuzuche.lib_zxing.DisplayUtil;
import com.zlzvip.android.BuildConfig;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class MyApplication extends Application {

    private static Context mAppContext = null;
    public static final String BUGLY_ID ="f172683cf8";

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this.getApplicationContext();
        DimensionUtil.init(mAppContext, getResources());
//        UIL.init(this);
        SecretCodeActivity.setBuildType(BuildConfig.BUILD_TYPE);

        /**
         * 初始化腾讯bugly
         */
        initBugly();

        /**
         * 初始化尺寸工具类
         */
        initDisplayOpinion();
    }

    private void initBugly() {
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(context, BUGLY_ID, false, strategy);

    }


    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }


    private void initDisplayOpinion() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        DisplayUtil.density = dm.density;
        DisplayUtil.densityDPI = dm.densityDpi;
        DisplayUtil.screenWidthPx = dm.widthPixels;
        DisplayUtil.screenhightPx = dm.heightPixels;
        DisplayUtil.screenWidthDip = DisplayUtil.px2dip(getApplicationContext(), dm.widthPixels);
        DisplayUtil.screenHightDip = DisplayUtil.px2dip(getApplicationContext(), dm.heightPixels);
    }

    public static Context getAppContext() {
        return mAppContext;
    }



}
